$(function(){

    var finishSurvey = function(data){
        $('#an-modal #formMainBody').html(data);
        $('#an-modal #sendButton').remove();
        $('#an-modal #quitButton').html('Close');
        $('#an-satelliteBody').html(data);
        $.cookie('an-htsurveydone', 1, { expires : 10 });
    };


    console.log('i am the asknicely app');
//    $('#asknicelyModal').modal('toggle');

    $("#an-surveyForm").submit(function() {
        console.log("submittingl...");
        var url = an_config.hostUrl + "/saveResponse"; // the script where you handle the form input.

        $.ajax({
            type: "POST",
            url: url,
            data: $("#an-surveyForm").serialize(), // serializes the form's elements.
            timeout : 1000,
            success: function(data)
            {
                finishSurvey(data);
            },
            error: function(x, t, m) {
                if(t==="timeout") {
                    finishSurvey("<p>The server timed out :(</p>");
                } else {
                    finishSurvey('<p>Something went wrong :(</p>');
                }
            }
        });

        return false; // avoid to execute the actual submit of the form.
    });

});


